<?php
class Html
{
	public static function pager($pager)
	{
		if(!is_array($pager)) return;
		$tmp = '<div id="pager">';
		$tmp .= ($pager['p_offset'] <=1)
		   ? '<a class="disabled pointer">首页</a> <a class="disabled pointer">上一页</a>'
		   : "<a href='".str_replace('{PAGE}',1,$pager['url'])."'>首页</a> "
		   . "<a href='".str_replace('{PAGE}',$pager['p_offset']-1,$pager['url'])."'>上一页</a>";
		$tmp .= '&nbsp;';
		$tmp .= ($pager['p_offset'] >= $pager['p_total'])
		   ? '<a class="disabled pointer">下一页</a> <a class="disabled pointer">尾页</a>'
		   : "<a href='".str_replace('{PAGE}',$pager['p_offset']+1,$pager['url'])."'>下一页</a> "
		   . "<a href='".str_replace('{PAGE}',$pager['p_total'],$pager['url'])."'>尾页</a>";
		$tmp .= "&nbsp;<span>{$pager['r_total']} [<span class='pointer underline' id='pgroll'> {$pager['p_offset']} </span>/ {$pager['p_total']} ]</span>";
		$tmp .= '</div>';
		echo "{$tmp}\n<script> pager('{$pager['url']}'); </script>";
	}
	
	public static function write($str, $len=null, $blank=false)
	{
		echo $len ? htmlspecialchars(mb_strcut($str, 0, $len, 'UTF-8')) :
			( $blank ? str_replace("\n",'<br/>',$str) : htmlspecialchars($str) );
	}

	/** write html tag select */
	public static function options($array, $name='', $val='', $text_key='', $id='', $tips=null, $ext='', $val_key='', $tips_val='')
	{
		if(is_array($name)){
			extract($name);
		}
		if($name) $ext .= " name='{$name}'";
		if($id) $ext .= " id='{$id}'";
		$html	= "<select {$ext}>\n";
		if($tips!==null) $html .= "<option value='{$tips_val}'>{$tips}</option>\n";
		if($text_key)
		{
			if($array) foreach($array as $key=>$t){
				$value	= $val_key ? $t[$val_key] : $key;
				$html	.= "<option value='{$value}'>{$t[$text_key]}</option>\n";
			}
		}
		else
		{
			if($array) foreach($array as $key=>$t){
				$value	= $val_key ? $t[$val_key] : $key;
				$html	.= "<option value='{$value}'>{$t}</option>\n";
			}
		}
		$html	.= '</select>';
		if($val){
			$html	.= "\n<script>\$('".($id?"#{$id}":"select[name={$name}]")."').val({$val});</script>";
		}
		return $html;
	}
	
	/*
	 * data, id, name, val, val_key, text_key, tips, ext
	 */
	public static function options2($params){
		$data	= fetch_a_array('data', $params);
		$id		= fetch_a_string('id', $params);
		$name	= fetch_a_string('name', $params);
		$val	= fetch_a_string('val', $params);
		$val_key	= fetch_a_char('val_key', $params);
		$text_key	= fetch_a_string('text_key', $params);
		$tips		= fetch_a_string('tips', $params);
		$tips_val	= fetch_a_string('tips_val', $params);
		$ext	= fetch_a_char('ext', $params);
		
		return self::options($data, $name, $val, $text_key, $id, 
				/*
				 * options里对tips做了null判断，fetch_a_string返回''，这里验证传值；
				 */
				(isset($params['tips'])?$tips:null)
				, $ext, $val_key, $tips_val);
	}

	public static function hidden($name, $value=''){
		return "<input type='hidden' name='$name' value='$value' />";
	}

}

?>
